package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.region.Region;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.region.IRegionService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 行政区域信息Controller
 * Created by weih on 2016-10-11.
 */
@Controller
@RequestMapping("/mdm/region")
public class RegionController extends BaseController {

    @Autowired
    IRegionService regionService;
    /**
     * 跳转新增行政区域页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/region/add";
    }
    /**
     * 新增行政区域信息
     * @param region 行政区域对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addRegion(@RequestBody Region region,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        region.setCreateUser(getSessionUser().getUserId());
        region.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            regionService.addRegion(region);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑行政区域页面
     * @param regionCode  行政区域代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String regionCode,Model model){
        model.addAttribute("region", regionService.getRegionByCode(regionCode));
        return "mdm/region/edit";
    }

    /**
     * 编辑行政区域信息
     * @param region
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editRegion(@RequestBody Region region,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Region oldRegion=regionService.getRegionByCode(region.getRegionCode());
        oldRegion.setId(region.getId());
        oldRegion.setRegionCode(region.getRegionCode());
        oldRegion.setRegionName(region.getRegionName());
        oldRegion.setParentId(region.getParentId());
        oldRegion.setStatus(region.getStatus());
        oldRegion.setRegionType(region.getRegionType());
        oldRegion.setPinyin(region.getPinyin());
        oldRegion.setQuanpin(region.getQuanpin());
        oldRegion.setLatitude(region.getLatitude());
        oldRegion.setLongitude(region.getLongitude());
        oldRegion.setRemark(region.getRemark());
        oldRegion.setUpdateUser(getSessionUser().getUserId());
        oldRegion.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            regionService.updateRegion(oldRegion);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除行政区域信息
     * @param region 行政区域对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteRegion(@RequestBody Region region,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        region.setUpdateUser(getSessionUser().getUserId());
        region.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            regionService.deleteRegion(region);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/region/list";
    }

    /**
     * 分页查询数据
     * @param region 行政区域对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Region> getRegionData(Region region ,DataTableRequest dtrequest){
        int cnt = (int)regionService.getRegionCount(region);
        List<Region> list = regionService.queryRegions(region, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Region> result = new PaginationResult<Region>(list, pi);
        return new DataTableResponse<Region>(dtrequest.getsEcho(),result);
    }

}
